Home:ALL Converter>how to reproduce "Connection reset by peer"

how to reproduce "Connection reset by peer"

Ask Time:2016-11-24T09:52:46         Author:Jiacai Liu

Json Formatter

I have learned the differences between the two infamous errors in tcp:

  • [Errno 54] Connection reset by peer
  • [Errno 32] Broken pipe

Both errors are one side of the tcp connection closed for unknown reason, and the other side still communicate with it.

  • when the other side write something, Broken pipe is thrown
  • when the other side read something, Connection reset by peer is thrown

I was able to reproduce Broken pipe using Python codes below.

# tcp_server.py
def handler(client_sock, addr):
    try:
        print('new client from %s:%s' % addr)
    finally:
        client_sock.close()   # close current connection directly

if __name__ == '__main__':

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', 5500))
    sock.listen(5)

    while 1:
        client_sock, addr = sock.accept()
        handler(client_sock, addr)

As for client,

>>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sock.connect(('', 5500))
>>> sock.send('a')
1
>>> sock.send('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: [Errno 32] Broken pipe

When the client first send, a RST packet is sent from server to client, from this moment on, send will always throw Broken pipe.

Everything above is within my understanding. However when client read from server , it always return empty string instead of throw Connection reset by peer

>>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sock.connect(('', 5500))
>>> sock.recv(1024)
''
>>> sock.recv(1024)
''
>>> sock.recv(1024)
''
>>> sock.recv(1024)

I am confused at this, or generally how to reproduce the Connection reset by peer ?

Author:Jiacai Liu,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40776913/how-to-reproduce-connection-reset-by-peer
yy